package com.wframe.project.jnd.services.impl;

import com.frame.database.JdataBean;
import com.wframe.project.jnd.dao.PaymentDao;
import com.wframe.project.jnd.entity.Payment;
import com.wframe.project.jnd.rest.util.ClientException;
import com.wframe.project.jnd.services.PayService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by Administrator on 2017/5/12.
 */
@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private PaymentDao paymentDao;

    @Override
    public Object findAttribute(String columnName, String key, Object value) {
        return paymentDao.findAttribute("wb_payment", columnName, key, value);
    }

    @Override
    @Transactional
    public void update(Map columnMap, Map keyMap) {
        try {
            paymentDao.update("wb_payment", columnMap, keyMap);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    @Transactional
    @Override
    public Payment generateOrder(Payment payment) {
        return this.paymentDao.generateOrder(payment);
    }

    @Transactional
    @Override
    public Payment getOrderByCode(String payCode) {
        Payment payment = paymentDao.getOrderByCode(payCode);
        return payment;
    }

    @Transactional
    @Override
    public List<JdataBean> getNotPayList() {
        String sql = "SELECT * FROM wb_payment pp WHERE ( pp.pay_state = 'SUBMITTED' OR pp.pay_state = 'GENERATED' ) AND pp.create_time > DATE_SUB(NOW(), INTERVAL 2 HOUR) ";
        Object[] params = new Object[]{};
        return paymentDao.querySql(sql, params);
    }

    @Override
    public Payment paymentRefund(Payment payment) {
        if (payment != null) {
            Payment readyPayment = getOrderByCode(StringUtils.trimToEmpty(payment.getSn()));
            if (readyPayment != null) {
                if (readyPayment.getPay_state() != Payment.PayState.SUCCESS) {
                    System.err.println("该订单状态不支持退款");
                } else {
                    if (readyPayment.getPay_way() == Payment.PayWay.WECHAT_MP || readyPayment.getPay_way() == Payment.PayWay.WECHAT_MP) {
                        //TODO 微信支付退款
                    } else {
                        System.err.println("该订单类型不支持退款：" + readyPayment.getPay_way());
                    }
                }
            }
        }
        return null;
    }

    @Transactional
    @Override
    public BigDecimal getBigDecimalAmout(String sn) throws ClientException {
        BigDecimal amoutAll = paymentDao.getAmoutAll(sn);
        return amoutAll;
    }

    @Override
    public Payment getOrder(Map keyMap) {
        JdataBean attributes = paymentDao.findAttributes("wb_payment", keyMap, "sn");
        if (attributes != null) {
            return getOrderByCode(attributes.getString("sn"));
        }
        return null;
    }

    @Override
    @Transactional
    public void updateSubmitted(String sn) {
        paymentDao.updateSubmitted(sn);
    }

}
